package dk.mirasola.systemtraining.user.client.ui;

import com.google.gwt.core.client.GWT;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.SimplePager;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.SingleSelectionModel;
import dk.mirasola.systemtraining.user.client.dataprovider.UserDistributionFilterDataProvider;
import dk.mirasola.systemtraining.user.client.i18n.UserDistributionFilterTableTexts;
import dk.mirasola.systemtraining.user.client.ui.table.STCellTableResource;
import dk.mirasola.systemtraining.user.shared.transfer.UserDistributionFilterTO;

public class DistributionFilterSelector extends Composite {
    interface DistributionFilterSelectorUiBinder extends UiBinder<Widget, DistributionFilterSelector> {
    }

    private static DistributionFilterSelectorUiBinder uiBinder = GWT.create(DistributionFilterSelectorUiBinder.class);
    private static UserDistributionFilterTableTexts texts = GWT.create(UserDistributionFilterTableTexts.class);

    @UiField(provided = true)
    CellTable<UserDistributionFilterTO> table;
    @UiField
    SimplePager pager;

    private UserDistributionFilterDataProvider dataProvider = new UserDistributionFilterDataProvider();
    private SingleSelectionModel<UserDistributionFilterTO> selectionModel = new SingleSelectionModel<UserDistributionFilterTO>();

    public DistributionFilterSelector() {
        initTable();
        initWidget(uiBinder.createAndBindUi(this));
        dataProvider.addDataDisplay(table);
        pager.setDisplay(table);
    }

    public SingleSelectionModel<UserDistributionFilterTO> getSelectionModel() {
        return selectionModel;
    }

    private void initTable() {
        CellTable.Resources resources = GWT.create(STCellTableResource.class);

        table = new CellTable<UserDistributionFilterTO>(5, resources);
        table.setSelectionModel(selectionModel);

        TextColumn<UserDistributionFilterTO> nameColumn = new TextColumn<UserDistributionFilterTO>() {
            @Override
            public String getValue(UserDistributionFilterTO userDistributionFilter) {
                return userDistributionFilter.getName();
            }
        };

        TextColumn<UserDistributionFilterTO> descColumn = new TextColumn<UserDistributionFilterTO>() {
            @Override
            public String getValue(UserDistributionFilterTO userDistributionFilter) {
                return userDistributionFilter.getDescription();
            }
        };

        table.addColumn(nameColumn, texts.nameColumn());
        table.addColumn(descColumn, texts.descriptionColumn());
    }

}

